<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * 每次扒取完所有数据后整理exhibitions表
 *
 */
class exhibitionsMakerController extends baseController{

    public function __construct()
    {
        parent::__construct();


    }

    public function index(){
        $count = model('maexhibitions')->count();
        echo $count;die;
        //SELECT * FROM table WHERE id >=2500 ORDER BY auto_id asc LIMIT 0,20



    }
    //需不需要分网站处理 第一时间只处理雅昌的数据

    //从introduction中提取tags，对比提取         tags AND keyword
    //从地址得到坐标                            坐标
    //城市省份归类                              城市省份完善
    //从address和organizers两项查机构表得到ID     museumID（galleries）

    /*
     * 通过address，organizers获取museumID，如果搜不到就新建一个museum并返回给他
     *
     *
     */
    public function getmuseumID($data,$flag=0){
        $address = $data['address'];
        $organizers = $data['organizers'];
        $id = model('companies')->find('apellation="'.$address.'"');
        /*echo 'theone';
        print_r($id);
        echo '<--->';*/
        if(!$id){
            $id = model('companies')->find('apellation="'.$organizers.'"');
            /*echo 'thetwo';
            print_r($id);
            echo '<--->';*/
        }
        if(!$id){
            if($flag=0) {
                $insert['apellation'] = $address;
            }else{
                $insert['apellation'] = $organizers;
            }
//            print_r($insert);die;
            if(!isset($insert['logo'])){
                $insert['logo'] = '';
            }
            if(!isset($insert['logourl'])){
                $insert['logourl'] = '';
            }
            if(!isset($insert['corporation'])){
                $insert['corporation'] = '';
            }
            if(!isset($insert['address'])){
                $insert['address'] = '';
            }
            if(!isset($insert['tel'])){
                $insert['tel'] = '';
            }
            if(!isset($insert['fax'])){
                $insert['fax'] = '';
            }
            if(!isset($insert['email'])){
                $insert['email'] = '';
            }
            if(!isset($insert['website'])){
                $insert['website'] = '';
            }
            if(!isset($insert['about'])){
                $insert['about'] = '';
            }
            if(!isset($insert['business'])){
                $insert['business'] = '';
            }
            if(!isset($insert['city'])){
                $insert['city'] = 0;
            }
            if(!isset($insert['longitude'])){
                $insert['longitude'] = '';
            }
            if(!isset($insert['latitude'])){
                $insert['latitude'] = '';
            }

            $idinfo = model('companies')->insert($insert);
            /*echo 'thethree';
            print_r($idinfo);
            echo '<--->';*/
            return $idinfo;
        }
        /*echo 'thefour';
        print_r($id);
        echo '<--->';*/
        return $id['id'];
    }

    /*
     * 根据address获取坐标，如果没有坐标就用所属馆的坐标，如果所属馆页没有坐标，那就没办法了
     *
     *
     */
    public function getlocation($data){
        $address = $data['address'];
        $museumID = $data['companiesID'];
        $organizers = $data['organizers'];
        $city = $data['city'];
        print_r($data);

        $encode = mb_detect_encoding($city, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
        echo '=====',$encode,'=====';
        $map = new mapController();
        if($address != null){
            echo '<---address--->';
            $location = $map->getlocation($address,$city);
            if($location != false){
                return $location;
            }
        }
        if($organizers != null){
            echo '<---organizers--->';
            $location = $map->getlocation($organizers,$city);
            if($location != false){
                return $location;
            }
        }
        if ($museumID != null){
            echo '<---museum--->';
            $info = model('companies')->find('id="'.$museumID.'"');
            $location['longitude'] = $info['longitude'];
            $location['latitude'] = $info['latitude'];
            if(($location['longitude']!=null || $location['longitude']!=''||$location['longitude']!=0)&& ($location['latitude']!=null || $location['latitude']!=''||$location['latitude']!=0)){
                return $location;
            }
        }
        return false;
    }

    /*
     * 根据 artist curator查表看能不能找到，找不到的就没办法了，前台设置一个通用的空界面，如果返回的为空，就调用作者名字和空页面，显示暂无该作者更多信息
     * 这里有个棘手的问题，名字里有好多都是多个名字，且中间没有分隔符的，这种情况必须得先处理，返回的结果应该是类似（作者名:ID,作者名:ID,......）
     */

    public function getauthorID($data,$flag=0){
        $curator ='';
        if($flag==0) {
            $curator = $data['curator'];
        }elseif($flag==1){
            $curator = $data['artist'];
        }
        if($curator == null){
            return false;
        }
        $pos = mb_strpos($curator, ',');
        if ($pos !== false) {
            $curatorarr = explode(',', $curator);
            $ret = '';
            for ($i = 0; isset($curatorarr[$i]); $i++) {
                $info = model('maartist')->find('name="' . $curatorarr[$i] . '"');

                if ($info['ID'] != null) {
                    $ret = $ret . $curatorarr[$i] . ':' . $info['ID'] . ',';
                }
            }
            return $ret;
        }
        $pos =mb_strpos($curator,'·');
        if($pos !==false){
            $info = model('maartist')->find('name="' . $curator . '"');
            if($info['ID'] !=null){
                return $curator.':'.$info['ID'].',';
            }
        }
        $len = mb_strlen($curator);
        if($len < 5){
            $info = model('maartist')->find('name="' . $curator . '"');
            if($info['ID'] !=null){
                return $curator.':'.$info['ID'].',';
            }
        }
        return false;
    }




}

